package network

import (
	"gitee.com/amoyx/cloud-provider/pkg/cloud/tencent/models"
	"github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common"
	"github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common/profile"
	tvpc "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/vpc/v20170312"
	"log"
)

type VpcGreeter interface {
	VPC(region string) VpcInterface
}

type VpcInterface interface {
	Create(request models.VpcRequest) (id string)
	Get(id string) (result models.VpcResponse, err error)
	Update()
	Delete(id string) bool
	ListByName(name string) (list []models.VpcResponse, err error)
	List() (list []models.VpcResponse, err error)
}

type vpc struct {
	network *Network
	region  string
	client  *tvpc.Client
}

func newVpc(network *Network, region string) VpcInterface {
	v := &vpc{
		network: network,
		region:  region,
	}
	cpf := profile.NewClientProfile()
	cpf.HttpProfile.Endpoint = string(VpcEndPoint)
	client, _ := tvpc.NewClient(v.network.Credential(), v.region, cpf)
	v.client = client
	return v
}

func (v *vpc) Create(in models.VpcRequest) (id string) {
	request := tvpc.NewCreateVpcRequest()
	request.VpcName = common.StringPtr(in.VpcName)
	request.CidrBlock = common.StringPtr(in.CidrBlock)
	var tags []*tvpc.Tag
	for key, val := range in.Tags {
		tags = append(tags, &tvpc.Tag{
			Key:   common.StringPtr(key),
			Value: common.StringPtr(val),
		})
	}
	response, err := v.client.CreateVpc(request)
	if err != nil {
		return
	}
	id = *response.Response.Vpc.VpcId
	return
}

func (v *vpc) Get(id string) (result models.VpcResponse, err error) {
	list, err := v.query(map[string]string{"vpc-id": id})
	if len(list) > 0 {
		return list[0], nil
	}
	return
}

func (v *vpc) Update() {

}

func (v *vpc) Delete(id string) bool {
	request := tvpc.NewDeleteVpcRequest()
	request.VpcId = common.StringPtr(id)
	_, err := v.client.DeleteVpc(request)
	if err != nil {
		return false
	}
	return true
}

func (v *vpc) List() (list []models.VpcResponse, err error) {
	return v.query(map[string]string{})
}

func (v *vpc) ListByName(name string) (list []models.VpcResponse, err error) {
	return v.query(map[string]string{"vpc-name": name})
}

func (v *vpc) query(args map[string]string) (list []models.VpcResponse, err error) {
	request := tvpc.NewDescribeVpcsRequest()
	var filters []*tvpc.Filter
	for key, val := range args {
		filters = append(filters, &tvpc.Filter{
			Name:   common.StringPtr(key),
			Values: common.StringPtrs([]string{val}),
		})
	}
	request.Filters = filters
	request.Limit = common.StringPtr("100")
	response, err := v.client.DescribeVpcs(request)

	if err != nil {
		log.Println("ERROR: 获取vpc数据失败", err.Error())
		return
	}

	for _, row := range response.Response.VpcSet {
		var dl []string
		for _, dns := range row.DnsServerSet {
			dl = append(dl, *dns)
		}
		list = append(list, models.VpcResponse{
			VpcName:         *row.VpcName,
			VpcId:           *row.VpcId,
			CidrBlock:       *row.CidrBlock,
			IsDefault:       *row.IsDefault,
			EnableMulticast: *row.EnableMulticast,
			CreatedTime:     *row.CreatedTime,
			DnsServerSet:    dl,
			DomainName:      *row.DomainName,
		})
	}
	return
}
